package ui 
{
	import Base.Loader.SWFLoader;
	
	import Common.GameDefine;
	import Common.GameResManager;
	import Common.RenderManager;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	import map.MapGrid;
	
	import net.PlayerNetHandler;

	/**
	 * ...
	 * @author ...
	 */
	public class MainUIHandler extends uiBase
	{
		private var topLeftUI:MovieClip = null;
		private var topRightUI:MovieClip = null;
		
		
		public function MainUIHandler() 
		{
			path = "mainUI.swf";
			name = "MainUI";
		}
		
		
		public override function onInit( loader:SWFLoader ):void
		{
			PlayerNetHandler.SendGetPet();
			
			var clas:Class = loader.getClass( name );
			uiMc = new clas();
			uiMc.name = name;
			GameUIManager.instance.addBottomUI( uiMc );
			
			uiMc.mapButton.addEventListener( MouseEvent.CLICK , onClickMapButton );
			uiMc.itemButton.addEventListener( MouseEvent.CLICK , onClickItemButton );
			uiMc.roleInfoButton.addEventListener( MouseEvent.CLICK , onClickRoleInfoButton );
			uiMc.petButton.addEventListener( MouseEvent.CLICK , onClickPetButton );
			uiMc.mailButton.addEventListener( MouseEvent.CLICK , onClickMailButton );
			uiMc.photoButton.addEventListener( MouseEvent.CLICK , onClickPhotoButton );
			
			clas = loader.getClass( "TopLeftUI" );
			topLeftUI = new clas();
			GameUIManager.instance.addBottomUI( topLeftUI );
			
			clas = loader.getClass( "TopRightUI" );
			topRightUI = new clas();
			topRightUI.battleButton.addEventListener( MouseEvent.CLICK , onBattle );
			GameUIManager.instance.addBottomUI( topRightUI );
			TopUIHandler.Instance().init( topLeftUI , topRightUI );
			
			clas = loader.getClass( "Alart" );
			AlartUIHandler.instance.init( new clas() );
			
			resize();
		}
		
		public override function resize():void
		{
			if ( uiMc )
			{
				uiMc.x = RenderManager.instance.stageWidth * 0.5;
				uiMc.y = RenderManager.instance.stageHeight;
				
				topLeftUI.x = 1;
				topLeftUI.y = 1;
				
				topRightUI.x = RenderManager.instance.stageWidth - 1;
				topRightUI.y = 1;
			}
			
		}
		
		public override function visible( b:Boolean ):void
		{
			if ( b )
			{
				if ( !uiMc )
				{
					GameResManager.instance.load( GameDefine.PATHUI + path , onInit );
					return;
				}
				else
				{
					if ( uiMc.visible )
					{
						return;
					}
					
					uiMc.visible = true;
					topLeftUI.visible = true;
					topRightUI.visible = true;
				}
			}
			else
			{
				if ( uiMc )
				{
					uiMc.visible = false;
					topLeftUI.visible = false;
					topRightUI.visible = false;
				}
				
			}
		}
		
		
		public static var instance:MainUIHandler = null;
		public static function Instance():MainUIHandler
		{
			if ( instance == null )
			{
				instance = new MainUIHandler();
			}
			
			return instance;
		}
		
		
		public function closeAll():void
		{
			ItemUIHandler.instance.visible( false );
			MapUIHandler.instance.visible( false );
			MailUIHandler.instance.visible( false );
			PetUIHandler.instance.visible( false );
			PhotoUIHandler.instance.visible( false );
		}
		
		public function onClickItemButton( e:MouseEvent ):void
		{
			MapUIHandler.instance.visible( false );
			ItemUIHandler.instance.visible( !ItemUIHandler.instance.isVisible );
		}
		
		public function onClickMapButton( e:MouseEvent ):void
		{
			ItemUIHandler.instance.visible( false );
			
			MapUIHandler.instance.visible( !MapUIHandler.instance.isVisible );
		}
		
		public function onClickRoleInfoButton( e:MouseEvent ):void
		{
			RoleInfoUIHandler.instance.visible( !RoleInfoUIHandler.instance.isVisible );
			
			MailUIHandler.instance.visible( false );
			PetUIHandler.instance.visible( false );
			PhotoUIHandler.instance.visible( false );
		}
		
		public function onClickPetButton( e:MouseEvent ):void
		{
			MailUIHandler.instance.visible( false );
			PetUIHandler.instance.visible( !PetUIHandler.instance.isVisible );
			RoleInfoUIHandler.instance.visible( false );
			PhotoUIHandler.instance.visible( false );
		}
		
		public function onClickMailButton( e:MouseEvent ):void
		{
			MailUIHandler.instance.visible( !MailUIHandler.instance.isVisible );
			PetUIHandler.instance.visible( false );
			RoleInfoUIHandler.instance.visible( false );
			PhotoUIHandler.instance.visible( false );
		}
		
		public function onClickPhotoButton( e:MouseEvent ):void
		{
			MailUIHandler.instance.visible( false );
			PetUIHandler.instance.visible( false );
			RoleInfoUIHandler.instance.visible( false );
			PhotoUIHandler.instance.visible( !PhotoUIHandler.instance.isVisible );
		}
		
		
		public function onBattle( e:MouseEvent ):void
		{
			var mainPlayer:GamePlayer = GameManager.instance.MainPlayer;
			
			var grid:MapGrid = mainPlayer.MMapGrid.clone();
			grid.move( mainPlayer.Direction() );
			
			var id:int = GameCreatureManager.instance.isPlayer( grid.x , grid.y );
			
			if ( id != GameDefine.INVALIDID )
			{
				PlayerNetHandler.SendBattle( 1 , id );
			}
		}
		
		
	}

}
